<?php

class EpisodeAddAction extends BaseAction 
{
	public $programme_id;
	/**
	 * @isnull false
	 * @filter positive_number
	 * @maxLength 9
	 * @label On Air Episode No.
	 */
	public $onair_episode_no;
	/**
	 * @maxLength 255
	 * @label Programme Title (Chinese)
	 */
	public $programme_title_cn;
	/**
	 * @maxLength 255
	 * @label Programme Title (English)
	 */
	public $programme_title_en;
	/**
	 * @isnull false
	 * @maxLength 255
	 * @label Programme Path
	 */
	public $programme_path;

	public $ut;
	public function execute(){
		if(count($this->_errors) > 0){
			$this->init_page_data();
			return 'filter-error';
		}

		Model::$dbo['w']->beginTransaction();
		try{
			//check name unique
			if (is_null($this->programme_title_cn) && is_null($this->programme_title_en))
			{
				$this->init_page_data();
				$this->addError('Programme.Programme Title', array('0037', 'Programme Title'));
				return 'filter-error';
			}

			//check programme exist?
			if (is_null($this->programme_id))
			{
				$this->addError('programme', array('0101','Progrmame'));
				$this->init_page_data();
				return 'logic-error';
			}

			//check programme and on air episode no
			if (episode::getEpisodeID($this->programme_id, $this->onair_episode_no) != '')
			{
				$this->addError('programme & on air episode no', array('0102','Programme & On Air Episode No'));
				$this->init_page_data();
				return 'logic-error';
			}

			# change_log
			$this->change_log = array();
			$insert_id = $this->add_episode();
			$insert_language_id = $this->add_episode_language($insert_id);
			# save change_log
			array_walk($this->change_log,'change_log::updateOne');

			Model::$dbo['w']->commit();

			$this->setData('id', $insert_id);
			$this->setData('act', 'add');
			$this->setData('step', 2);
			return 'succ';
		}
		catch(Exception $e)
		{
			error_log("Add Episode Fails: " . var_export($this, 1));
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}    

	/**
	 * add episode base information
	 */
	private function add_episode()
	{
		$episode = new episode();
		$episode->set($this);
		$episode->created_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$episode->created_by = CommonUtils::get_user();
        $episode->modified_at = $episode->created_at;
        $episode->modified_by = $episode->created_by;
		$episode->folders = CommonUtils::init_folder("episode", $this);
		$episode->available_tag_types = CommonUtils::get_allow_tags("episode");
		$episode->highlighted_cast_roles = CommonUtils::get_highlight_role("episode");
		$episode->status = AC::STATUS_NEW;
		$episode->casts = $this->fetch_programme_cast();

		$insert_id = Model::$dbo['w']->save($episode);
		if ($insert_id > 0)
		{
			$this->_log_programme_id = 0 ;            // for change_log in CommonUtils init_folder function.At the proofread programme time needn't proofread episode.
			$this->_log_episode_id   = $insert_id;    // for change_log in CommonUtils init_folder function
			$this->change_log[] = array(
				'type'=>'episode',
				'keys'=>array('id'=>$insert_id),
				'is_active'=>1,
				'is_publish'=> 0,
				'episode_id'=>array($insert_id)
			);

			$this->addInfo("episode:".AC::LOG_OP_CREATE.":".$insert_id, "add episode success", json_encode($episode));
			return $insert_id;
		}
		else
		{
			$this->addError("episode:".AC::LOG_OP_CREATE.":".$insert_id, "add episode error", json_encode($episode));
			return false;
		}

	}

	/**
	 * add episode language 
	 */
	private function add_episode_language($episode_id)
	{
		$episode_language = new episode_language();
		$episode_language->set($this);
		$episode_language->episode_id = $episode_id;
		$episode_language->language_id = language::getLanguageID(AC::DEFAULT_LANGUAGE);

		$ret = Model::$dbo['w']->save($episode_language);
		if ($ret >= 0)
		{
			$this->change_log[] = array(
				'type'=>'episode-language',
				'keys'=>array('episode_id'=>$episode_id,'language_id'=>$episode_language->language_id), 
				'is_active'=>1,
				'is_publish'=> 0,
				'episode_id'=>array($episode_id)
			);
			$this->addInfo("episode-language:".AC::LOG_OP_CREATE.":".$episode_id, "add episode language success", json_encode($episode_language));
		}
		else
		{
			$this->addError("episode-language:".AC::LOG_OP_CREATE.":".$episode_id, "add episode language error", json_encode($episode_language));
		}
	}

	/**
	 * fetch programme's cast to fill episode
	 */
	private function fetch_programme_cast()
	{
		$programme = new programme();
		$programme_detail = $programme->get($this->programme_id);

		return $programme_detail->casts;
	}

	/**
	 * init page data
	 */
	private function init_page_data()
	{
		$this->setData('onair_episode_no', $this->onair_episode_no);
		$this->setData('programme_title_cn', $this->programme_title_cn);
		$this->setData('programme_title_en', $this->programme_title_en);
		$this->setData('programme_path', $this->programme_path);
		$this->setData('programme_id', $this->programme_id);

		//jump page
		$this->setData('type', 'episode');
		$this->setData('act', 'add');
		$this->setData('step', 1);
	}
}
?>
